package com.lwd.util;

import java.util.Collection;

/**
 * @author Brozen
 * @date 2019/6/11 3:07 PM
 */
public class MathUtils {

    /**
     * 对于long，可能存在精度丢失
     */
    public static <T extends Number> T min(Collection<T> numbers) {
        T min = null;
        for (T number : numbers) {
            if (min == null) {
                min = number;
                continue;
            }

            if (number instanceof Comparable && ((Comparable) number).compareTo(min) < 0) {
                min = number;
                continue;
            }

            double minDouble = min.doubleValue();
            double numberDouble = number.doubleValue();
            min = (numberDouble <= minDouble) ? number : min;
        }
        return min;
    }



}
